// xc_lshop/club/barter/pro/index.js
var actclub_barter = require('../../model/club_barter')

Page({
  mixins: [require('../../mixins/clubMixin')],

  /**
   * 页面的初始数据
   */
  data: {
    /*表单验证是否通过数据*/
    vildfield: {
      price: true,
      name: true,
    },
    /* 表单提交的数据*/
    formdata: {},
    fileList: [],
  },
  afterclubinfo: function (options, config, userinfo, clubinfo) {
    var that = this;

    // this.deailerdata("0100_70")

    if (options['ordernum']) {
      this.getorderinfo(options['ordernum'])
    } else {
      this.opscan();
    }



  },
  //获取订单详情
  getorderinfo(ordernum) {
    var that = this;

    actclub_barter.orderinfo(ordernum).then(function (res) {
      if (res["status"] == 1) {
        console.log(res['obj'])
        var obj=res['obj'];
        var formdata=that.data.formdata; 
        formdata['message']=obj['message'];
        formdata['name']=obj['name'];
        formdata['price']=(obj['price'] / 100).toFixed(2);
        formdata['uid']=obj['uid'];
        formdata['imglist']=obj['imglist'];
        var fileList=[];
        for(var i in obj['imglist']){
          fileList.push({path:obj['imglist'][i],url:obj['imglist'][i]})
        }
        that.setData({
          formdata,
          fileList
        })
      } else {
        that.error(res["message"])
      }
    })

  },
  btnop(e) {
    var op = e.detail.op;
    if (op == 1) {
      var formdata = this.data.formdata;
      formdata['name'] = "";
      formdata['price'] = "";
      formdata['message'] = "";
      formdata['imglist'] = [];

      this.setData({
        formdata,
        fileList: []
      })
    } else {
      this.pageback()
    }

  },
  deailerdata(vcode) {
    var that = this;
    var arrvcode = vcode.split("_");


    switch (arrvcode[0]) {
      case "0100": {
        this.setData({
          'formdata.uid': arrvcode[1]
        })
        break;
      }
      default: {
        that.showerr()
      }
    }
  },

  showerr() {
    var that = this;
    wx.showModal({
      title: '错误',
      content: "未识别改用户信息",
      confirmText: "重新扫码",
      cancelText: "返回",
      complete: (res) => {
        if (res.cancel) {
          that.pageback()
        }

        if (res.confirm) {
          that.opscan();
        }
      }
    })
  },

  opscan: function (e) {
    var that = this;
    wx.scanCode({
      onlyFromCamera: false,
      success: function (reslut) {
        var vcode = ""
        if (reslut.result) {
          vcode = reslut.result
        } else if (reslut.path) {
          vcode = reslut.path
        } else {
          return
        }
        if (vcode) {
          //"0022", "2020202011112626112506"
          //that.deailerdata("0021_2020202011112323195147") 
          // that.deailerdata("0022_2121212103031010180111") 
          that.deailerdata(vcode)
        }
      },
      fail() {
        that.showerr()
      },
      complete: function (com) {


      }
    })
  },
  // 表单提交
  formSubmit: function (e) {
    var that = this;
    var valid = this.checkForm();
    console.log(valid)
    if (valid) {
      actclub_barter.applyform(that.data.formdata).then(function (res) {
        if (res["status"] == 1) {
          that.selectComponent("#examstatus").init(res["examstatus"])
        } else {
          that.error(res["message"])
        }
      })
    }
    console.log(valid)
  },
  //验证规则
  checkRule(field) {
    var updatemodel = {};
    if (field == "sex") {
      updatemodel['vildfield.' + field] = (this.data.formdata[field] == 1 || this.data.formdata[field] == 2) ? true : false;
    } else {
      updatemodel['vildfield.' + field] = this.data.formdata[field] ? true : false;
    }
    this.setData(updatemodel)
  },
  //验证表单
  checkForm: function (field) {
    var error = true;
    if (field) {
      this.checkRule(field)
    } else {
      ["name", "price"].forEach(item => {
        this.checkRule(item)
        if (!this.data.vildfield[item]) {
          error = false;
        }
      })
    }
    return error;
  },
  //监听input输入
  watchInput: function (e) {
    var dataset = e.target.dataset
    this.data.formdata[dataset.field] = e.detail
    this.checkForm(dataset.field)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  savefilefunc(file) {
    var app = getApp()
    var that = this
    app.toolv2.uploadFile(file.path).then(function (item) {
      var fileList = that.data.fileList;
      fileList.push({
        ...file,
        url: item.obj.fullpath
      });
      console.log(fileList)
      that.setData({
        fileList
      });
      that.data.formdata.imglist = [];
      fileList.forEach(item => {
        that.data.formdata.imglist.push(item.url)
      })
    })

  },
  afterRead(event) {

    var that = this
    const {
      file
    } = event.detail
    if (file.constructor == Array) {
      file.forEach(item => {
        that.savefilefunc(item)
      })
    } else {
      that.savefilefunc(file)
    }
  },


  afterdelete(e) {

    var that = this;
    var fileList = that.data.fileList
    var index = e.detail.index;

    fileList.splice(index, 1);

    this.setData({
      fileList: fileList,
    });
    var imglist = [];
    fileList.forEach(item => {
      imglist.push(item.url)
    })
    that.data.formdata.imglist = imglist

  },
  //上传图片--传图--获取url
  getUrlParam(url, name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象  
    var r = url.split('?')[1].match(reg); //匹配目标参数  
    if (r != null) return unescape(r[2]);
    return null; //返回参数值  
  },
})